package com.cjlgb.design.common.security.handler;

import com.cjlgb.design.common.security.util.SpringSecurityUtil;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.util.PatternMatchUtils;
import org.springframework.util.StringUtils;

/**
 * @author WFT
 * @date 2019/7/16
 * description: 权限校验处理器
 */
public class PermissionHandler {

    public boolean hasPermission(String ...permissions){
        Authentication authentication = SpringSecurityUtil.getAuthentication();
        if (null == authentication){
            return false;
        }
        return authentication.getAuthorities().stream()
                .map(GrantedAuthority::getAuthority)
                .filter(StringUtils::hasText)
                .anyMatch(x -> PatternMatchUtils.simpleMatch(permissions, x));
    }

}
